home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / news / nntp / nntp.1.5.11 / server / scandir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-08  |  2.1 KB  |  104 lines

  1. #ifndef lint
  2. static char    *sccsid = "@(#)$Header: scandir.c,v 1.6 91/02/08 18:22:07 sob Exp $";
  3. #endif
  4.  
  5. #include "common.h"
  6.  
  7. /*
  8.  * scan_dir -- scan the current directory for news articles,
  9.  *    loading the article numbers into art_array.  Return
  10.  *    number of articles loaded.
  11.  *
  12.  *    Parameters:    "low_msg", "high_msg" are the low
  13.  *            and high messages numbers in this
  14.  *            group; we ignore numbers outside this
  15.  *            range.
  16.  *
  17.  *    Returns:    Number of articles loaded into
  18.  *            array.
  19.  *
  20.  *    Side effects:    Changes "art_array".
  21.  */
  22.  
  23. extern    int    intcmp();
  24. extern char *malloc(), *realloc();
  25.  
  26. scan_dir(low_msg, high_msg)
  27. int    low_msg, high_msg;
  28. {
  29.     register struct direct    *dirent;
  30.     register DIR        *dirp;
  31.     int            artnum;
  32.  
  33.     num_arts = 0;
  34.  
  35.     dirp = opendir(".");
  36.  
  37.     if (dirp == NULL)
  38.         return (0);
  39.  
  40.     while ((dirent = readdir(dirp)) != NULL) {
  41.         artnum = atoi(dirent->d_name);
  42. #ifdef DYNAMIC_ART_ARRAY
  43.         if (artnum == 0 || artnum < low_msg || artnum > high_msg)
  44.             continue;
  45.         /* Expand/allocate art_array elements as necessary */
  46.         if (num_arts + 1 >= size_art_array) {
  47.             size_art_array += 1024;
  48.             if (art_array) {
  49. #ifdef SYSLOG
  50.                 syslog(LOG_INFO,
  51.                     "increasing art_array to %d elements",
  52.                     size_art_array);
  53. #endif
  54.                 art_array = (int *)realloc(art_array,
  55.                     size_art_array * sizeof(*art_array));
  56.             } else
  57.                 art_array = (int *)
  58.                     malloc(size_art_array * sizeof(*art_array));
  59.             if (art_array == 0) {
  60. #ifdef SYSLOG
  61.                 syslog(LOG_ERR,
  62.                     "scan_dir(): malloc/realloc failed");
  63. #endif
  64.                 num_arts = 0;
  65.                 size_art_array = 0;
  66.                 size_art_array = 0;
  67.                 closedir(dirp);
  68.                 return(0);
  69.             }
  70.         }
  71.         art_array[num_arts] = artnum;
  72.          ++num_arts;
  73. #else
  74.         if (artnum != 0 && artnum >= low_msg && artnum <= high_msg)
  75.             art_array[num_arts++] = artnum;
  76. #endif
  77.  
  78.     }
  79.     closedir(dirp);
  80.  
  81.     qsort((char *) art_array, num_arts, sizeof(int), intcmp);
  82.  
  83.     return (num_arts);
  84. }
  85.  
  86.  
  87. /*
  88.  * intcmp -- compare to integers.
  89.  *
  90.  *    Parameters:    "x", "y" point to the integers to be compared.
  91.  *
  92.  *    Returns:    -1 if "x" is less than "y",
  93.  *            0 if "x" equals "y", and
  94.  *            1 if "x" is greater than "y".
  95.  *
  96.  *    Side effects:    None.
  97.  */
  98.  
  99. intcmp(x, y)
  100. register int    *x, *y;
  101. {
  102.     return (*x - *y);
  103. }
  104.